#pragma once
#include <osg/Notify>
#include <iostream>
#ifdef __ANDROID__
	#include <android/log.h>
#endif

#define OLogI OSG_NOTICE
#define OLogW OSG_WARN
#define OLogE OSG_FATAL

#define OLogEND << std::endl;

class OLog : public osg::NotifyHandler {
public:
	OLog() = default;

	/**
	 *@brief Level: ALWAYS > DEBUG_FP > DEBUG_INFO > INFO > NOTICE > WARN > FATAL
	 *@link https://codedocs.xyz/openscenegraph/OpenSceneGraph/a02634.html
	 *@warning official docs: "It is called when notification stream needs to be synchronized (i.e. after osg::notify() << std::endl)."
	 */
	void notify(osg::NotifySeverity severity, const char* msg) override {
		if (osg::INFO == severity)// disable `osg::INFO` osg-inner-default log print
			return;
		if (msg != NULL && msg[0] == '\0')
			return;
		const char* tag = "";
		switch (severity) {
			case osg::ALWAYS: tag = ""; break;
			case osg::FATAL: tag = "E"; break;
			case osg::WARN: tag = "W"; break;
			case osg::NOTICE:
			case osg::INFO: tag = "I"; break;
			case osg::DEBUG_INFO: tag = "D"; break;
			case osg::DEBUG_FP: tag = "D_FP"; break;
			default: tag = ""; break;
		}
		#ifdef __ANDROID__
			std::string TAG = "[";
			TAG += tag;
			TAG += "]";
			__android_log_print(ANDROID_LOG_WARN, TAG, "%s", msg);
			return;
		#endif
		std::cout << "[" << tag << "] " << msg;
	}

	static void setNotifySeverity(osg::NotifySeverity logLevel = osg::NOTICE) {
		osg::setNotifyHandler(new OLog);
		osg::setNotifyLevel(logLevel);
	}
};